// pages/cart/cart.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carList:[],//购物车列表
    cartFlag:true,
    page:1,
    showCount:10,
    total:0,//总金额
    delFlag:false,
    url:'../../images/selected2.png',
    url2:'../../images/selected.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const _this = this
    wx.request({
      url: app.globalData.url +'/shop/car/list',
      method:'post',
      header:{
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        currentPageForApp:1,
        showCount:10
      },
      success(res) {
        console.log('购物车列表',res)
        let Olist = res.data.shopcars
        Olist.forEach(function(value,index) {
          value.url = JSON.parse(value.images)[0].imageUrl
          value.flag = true
          value.cars2 = JSON.parse(value.cars)
          value.cars2.forEach(function(value2,index2) {
            value2.flag2 = true
          })
        })

        _this.setData({
          carList: Olist
        })
        console.log("格式化后的购物车列表",_this.data.carList)
        _this.Settlement()
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goIndex(res) {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  selectedAll() {
    const _this = this
    let Olist = _this.data.carList
    _this.setData({
      cartFlag: !_this.data.cartFlag
    })
    if (_this.data.cartFlag){
      Olist.forEach(function(value,index) {
        value.flag = true
        value.cars2.forEach(function(value2,index2) {
          value2.flag2 = true
        })
      })
    }else{
      Olist.forEach(function (value, index) {
        value.flag = false
        value.cars2.forEach(function (value2, index2) {
          value2.flag2 = false
        })
      })
    }
    _this.setData({
      carList: Olist
    })
    _this.Settlement()
  },
  //选择全部
  selectedAllIn(e) {
    const indx = e.currentTarget.dataset.index
    const _this = this
    let list = _this.data.carList
    list[indx].flag = !list[indx].flag
    _this.setData({
      carList:list
    })
    _this.checkSelect(indx, 'isAll')
    _this.Settlement()
  },
  less(e) {
    const _this = this
    const indx = e.currentTarget.dataset.index
    const indx2 = e.currentTarget.dataset.index2
    console.log('index', indx)
    console.log('index2',indx2)
    let Olist = _this.data.carList
    if (Olist[indx].cars2[indx2].carNum>1){
      Olist[indx].cars2[indx2].carNum--
    }
    _this.setData({
      carList: Olist
    })
    let shopCarId = _this.data.carList[indx].cars2[indx2].shopCarId
    let productId = _this.data.carList[indx].productId
    let productSpecsId = _this.data.carList[indx].cars2[indx2].productSpecsId
    let carNum = 1
    wx.request({
      url: app.globalData.url + '/shop/car/modify',
      method: 'post',
      header: {
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        shopCarId,
        productId,
        productSpecsId,
        carNum,
        operator:'del'
      },
      success(res) {
        console.log('购物车页面减少商品数量',res)
        _this.Settlement()
      }
    })
    console.log('减少商品', _this.data.carList)
  },
  add(e) {
    const _this = this
    const indx = e.currentTarget.dataset.index
    const indx2 = e.currentTarget.dataset.index2
    let Olist = _this.data.carList
    Olist[indx].cars2[indx2].carNum++
    _this.setData({
      carList: Olist
    })
    let shopCarId = _this.data.carList[indx].cars2[indx2].shopCarId
    let productId = _this.data.carList[indx].productId
    let productSpecsId = _this.data.carList[indx].cars2[indx2].productSpecsId
    let carNum = 1
    wx.request({
      url: app.globalData.url + '/shop/car/modify',
      method: 'post',
      header: {
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data: {
        shopCarId,
        productId,
        productSpecsId,
        carNum,
        operator: 'add'
      },
      success(res) {
        console.log('购物车页面增加商品数量', res)
        _this.Settlement()
      }
    })
    console.log('增加商品',_this.data.carList)
  },
  //规格选择
  selectedSingle(e){
    const _this = this
    const indx = e.currentTarget.dataset.index
    const indx2 = e.currentTarget.dataset.index2
    let Olist = _this.data.carList
    // console.log(Olist)
    Olist[indx].cars2[indx2].flag2 = !Olist[indx].cars2[indx2].flag2
    console.log(Olist[indx].cars2[indx2].flag2)
    _this.setData({
      carList: Olist
    })
    _this.Settlement()
    _this.checkSelect(indx)
  },
  //购物车列表是否勾选(组)
  checkSelect(e,v) { 
    const _this = this
    let Olist = this.data.carList
    let cc = 0 //当所有选中时组全选也选中
    let c = 0 //当所有选中时整体全选选中
    if(v == 'isAll'){
      if (Olist[e].flag){
        Olist[e].cars2.forEach(function (item, index) {
          item.flag2 = true
        })
      }else{
        Olist[e].cars2.forEach(function (item, index) {
          item.flag2 = false
        })
      }
      setTimeout(function () {
        Olist.forEach(function (value, index) {
          if (value.flag) {
            c++
          }
        })
        if (Olist.length == c) {
          _this.setData({
            cartFlag: true
          })
        } else {
          _this.setData({
            cartFlag: false
          })
        }
      }, 100)
    }else{
      Olist[e].cars2.forEach(function (item, indx) {
        if (item.flag2) {
          cc++
        }
      })
      setTimeout(function() {
        Olist.forEach(function (value, index) {
          if (value.flag) {
            c++
          }
        })
        if (Olist.length == c) {
          _this.setData({
            cartFlag:true
          })
        }else{
          _this.setData({
            cartFlag: false
          })
        }
      },100)

      if (Olist[e].cars2.length == cc) {
        Olist[e].flag = true
      } else {
        Olist[e].flag = false
      }
    }
    console.log('组全选', Olist[e].flag)
    this.setData({
      carList: Olist
    })
  },
  // 计算总金额
  Settlement() {
    const Olist = this.data.carList
    const _this = this
    let total = 0
    Olist.forEach(function(item2,index2) {
      item2.cars2.forEach(function(value3,index3) {
        if (value3.flag2){
          total += value3.carNum * value3.currentPrice
        }
      })
    })
    console.log('总金额',total)
    _this.setData({
      total
    })
  },
  //删除商品
  delete() {
    this.setData({
      delFlag: !this.data.delFlag
    })
  },
  //购物车提交
  goOrder() {
    const _this = this
    let Olist = _this.data.carList
    let Oarr = []
    let Ostring = ''
    Olist.forEach(function(value,index){
      value.cars2.forEach(function(value2,index2) {
        if (value2.flag2){
          Oarr.push(value2.shopCarId)
        }
      })
    })
    Ostring = Oarr.toString()
    console.log('商品集合',Ostring)
    if (!_this.data.delFlag){//结算
      wx.navigateTo({
        url: '/pages/order/order?list=' + Ostring + '&price=' + _this.data.total
      })
    }else{//删除
      wx.request({
        url: app.globalData.url +'/shop/car/delete',
        method:'post',
        header:{
          "content-type": 'application/x-www-form-urlencoded',
          "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
          "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
        },
        data:{
          shopCarId: Ostring
        },
        success(res) {
          console.log('删除成功',res)
          let Olist = _this.data.carList
          Olist.forEach(function(value,index) {
            if(value.flag){
              Olist.splice(index,1)
            }else{
              value.cars2.forEach(function(value2,index2) {
                if (value2.flag2){
                  Olist[index].cars2.splice(index2,1)
                }
              })
            }
          })
          _this.setData({
            carList: Olist
          })
        }
      })
    }
  }
})